//
//  DynamicImageView.m
//  Qianbancaifu
//
//  Created by SYLH on 16/4/28.
//  Copyright © 2016年 SYLH. All rights reserved.
//

#import "DynamicImageView.h"
#import "FullScreenImage.h"

@implementation DynamicImageView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {

        
        self.imageViewArray = [NSMutableArray array];
        
        CGFloat width = (CGRectGetWidth(self.bounds) - 12) / 3;
        for (int i = 0; i < 9 ; i++) {
            UIImageView * imageView = [[UIImageView alloc] initWithFrame:CGRectMake((width + 6) * (i % 3),5 + (width + 6) * (i / 3), width, width)];
            imageView.contentMode = UIViewContentModeScaleAspectFill;
            imageView.clipsToBounds = YES;
            imageView.tag = 10 + i;
            imageView.userInteractionEnabled = YES;
            imageView.exclusiveTouch = YES;
            [self addSubview:imageView];
            //点击手势识别器
            UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(processGesture:)];
            [imageView addGestureRecognizer:tap];
            
            [self.imageViewArray addObject:imageView];
        }

    }
    return self;
}

-(void)setDynamicModel:(DynamicModel *)dynamicModel
{
    _dynamicModel = dynamicModel;
    
    self.imageArray = _dynamicModel.imageList;
}

-(void)setImageArray:(NSArray *)imageArray
{
    _imageArray = imageArray;
    
    if (imageArray.count == 1) {
        UIImageView * imageView = self.imageViewArray[0];
        imageView.frame = CGRectMake(0, 5 , CGRectGetWidth(self.bounds) , CGRectGetHeight(self.bounds) - 5);

    }else if(imageArray.count == 2){
        CGFloat width = (CGRectGetWidth(self.bounds) - 6) / 2;
        
        UIImageView * imageView1 = self.imageViewArray[0];
        UIImageView * imageView2 = self.imageViewArray[1];
        imageView1.frame = CGRectMake(0 , 5, width, CGRectGetHeight(self.bounds));
        imageView2.frame = CGRectMake(width + 6 , 5, width, CGRectGetHeight(self.bounds));
        
    }else if(imageArray.count > 2){
        CGFloat width = (CGRectGetWidth(self.bounds) - 12) / 3;
        
        UIImageView * imageView1 = self.imageViewArray[0];
        UIImageView * imageView2 = self.imageViewArray[1];
        imageView1.frame = CGRectMake(0 , 5, width, width);
        imageView2.frame = CGRectMake(width + 6 , 5, width, width);
    }

    int i = 0;
    for (NSString * imageString in imageArray) {
        UIImageView * imageView = self.imageViewArray[i];;
        imageView.hidden = NO;
        
        [imageView sd_setImageWithURL:LOADINGTHUMBIMG_URL(imageString,400,400)
                             placeholderImage:[UIImage imageNamed:@"placeholder"]
                                      options:SDWebImageRetryFailed
                                    completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {

                                    }];
        i++;
    }
    for (int j = i; j < 9; j++) {
        UIImageView * imageView = self.imageViewArray[j];
        imageView.hidden = YES;
    }
}

#pragma mark - 手势
- (void) processGesture:(UISwipeGestureRecognizer *)gesture
{
    UIMenuController * menu = [UIMenuController sharedMenuController];
    [menu setMenuVisible:NO animated:YES];
    
    UIView * gesView = gesture.view;
    NSMutableArray * fullImageArray = [NSMutableArray array];
    
    int i = 0;
    for (NSString * imageString in self.imageArray) {
        FullScreenImage * imageModel = [FullScreenImage new];
        imageModel.fullImagePath = imageString;
        imageModel.showImageView = self.imageViewArray[i];
        [fullImageArray addObject:imageModel];
        i ++;
    }
    
    dispatch_async(dispatch_get_main_queue(), ^{
        
        DealPhotosView * dealPhotosView = [[DealPhotosView alloc] init];
        [dealPhotosView setFullScreenWithImageArray:fullImageArray showIndex:gesView.tag - 10];
        [[[UIApplication sharedApplication].delegate window] addSubview:dealPhotosView];
    });
}


-(void)clickImage:(UIImageView*)sender
{
    
}

@end
